package com.shaobufan.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaobufan.museum.mapper.MuseumExhibitMapper;
import com.shaobufan.museum.pojo.entity.MuseumExhibit;
import com.shaobufan.museum.pojo.vo.MuseumExhibitVo;
import com.shaobufan.museum.service.MuseumExhibitService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 17126
 * @description 针对表【museum_exhibit(博物馆展览)】的数据库操作Service实现
 * @createDate 2024-04-07 11:07:06
 */
@Service
public class MuseumExhibitServiceImpl extends ServiceImpl<MuseumExhibitMapper, MuseumExhibit>
        implements MuseumExhibitService {

    @Resource
    MuseumExhibitMapper museumExhibitMapper;

    /**
     * 通过 name获取展览信息
     *
     * @param exName 展览名称
     * @return 展览列表
     */
    @Override
    public List<MuseumExhibitVo> searchExhibitByName(String exName) {
        if (ObjectUtils.isNotEmpty(exName)) {
            QueryWrapper<MuseumExhibit> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ex_name", exName);
            return museumExhibitMapper.selectList(queryWrapper).stream().map(this::getMuseumExhibitVo).collect(Collectors.toList());
        }
        return museumExhibitMapper.selectList(null).stream().map(this::getMuseumExhibitVo).collect(Collectors.toList());
    }

    /**
     * 通过 id获取展览信息
     *
     * @param exId 展览 id
     * @return MuseumExhibitRequest
     */
    @Override
    public MuseumExhibitVo searchExhibitById(Integer exId) {
        QueryWrapper<MuseumExhibit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ex_id", exId);
        return getMuseumExhibitVo(museumExhibitMapper.selectOne(queryWrapper));
    }

    /**
     * 添加展览
     *
     * @param exName   名称
     * @param exDetail 详情
     * @param exImage  图片
     * @param exStart  开始时间
     * @param exEnd    结束时间
     * @return Boolean
     */
    @Override
    public Boolean addExhibit(
            String exName,
            String exDetail,
            String exImage,
            LocalDate exStart,
            LocalDate exEnd
    ) {
        MuseumExhibit museumExhibit = getMuseumExhibit(exName, exDetail, exImage, exStart, exEnd);
        int inserted = museumExhibitMapper.insert(museumExhibit);
        return inserted == 1;
    }

    public MuseumExhibitVo getMuseumExhibitVo(MuseumExhibit museumExhibit) {
        MuseumExhibitVo museumExhibitVo = new MuseumExhibitVo();
        museumExhibitVo.setExId(museumExhibit.getExId());
        museumExhibitVo.setExName(museumExhibit.getExName());
        museumExhibitVo.setExDetail(museumExhibit.getExDetail());
        museumExhibitVo.setExImage(museumExhibit.getExImage());
        museumExhibitVo.setExStart(museumExhibit.getExStart());
        museumExhibitVo.setExEnd(museumExhibit.getExEnd());

        return museumExhibitVo;
    }

    public MuseumExhibit getMuseumExhibit(
            String exName,
            String exDetail,
            String exImage,
            LocalDate exStart,
            LocalDate exEnd) {
        MuseumExhibit museumExhibit = new MuseumExhibit();
        museumExhibit.setExName(exName);
        museumExhibit.setExDetail(exDetail);
        museumExhibit.setExImage(exImage);
        museumExhibit.setExStart(exStart);
        museumExhibit.setExEnd(exEnd);

        return museumExhibit;
    }

    public MuseumExhibit getMuseumExhibit(
            Integer exId,
            String exName,
            String exDetail,
            String exImage,
            LocalDate exStart,
            LocalDate exEnd) {
        MuseumExhibit museumExhibit = getMuseumExhibit(exName, exDetail, exImage, exStart, exEnd);
        museumExhibit.setExId(exId);
        return museumExhibit;
    }
}